package com.two.screen.water.balance.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.EditText
import android.widget.Switch
import butterknife.BindView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.listener.MyTextNumberWatcher
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.ui.fragment.base.BaseFragment
import com.two.screen.water.balance.utils.KeyboardUtil
import com.two.screen.water.balance.utils.Params
import com.two.screen.water.balance.utils.SPUtils

@SuppressLint("NonConstantResourceId", "UseSwitchCompatOrMaterialCode")
class SystemSettingFragment() : BaseFragment() {
    override fun setLayoutResouceId(): Int = R.layout.fragment_system_info

    @BindView(R.id.mIsOpenVoiceBroadcastSw) protected lateinit var mIsOpenVoiceBroadcastSw: Switch
    @BindView(R.id.mIsOpenPrintSw) protected lateinit var mIsOpenPrintSw: Switch
    @BindView(R.id.mPrintTimesEt) protected lateinit var mPrintTimesEt: EditText

    override fun initView() {

    }

    override fun initData(arguments: Bundle?) {
        mIsOpenVoiceBroadcastSw.isChecked = DataApi.mIsOpenVoiceBroadcast
        mIsOpenPrintSw.isChecked = DataApi.mIsOpenPrint
        mPrintTimesEt.setText(DataApi.mPrintTimes.toString())
        KeyboardUtil.setSelected(mActivity, mPrintTimesEt, false)
    }

    override fun initListener() {
        mIsOpenVoiceBroadcastSw.setOnCheckedChangeListener { buttonView, isChecked ->
            DataApi.mIsOpenVoiceBroadcast = isChecked
            SPUtils.putBoolean(Params.mIsOpenVoiceBroadcastSw, isChecked)
        }

        mIsOpenPrintSw.setOnCheckedChangeListener { buttonView, isChecked ->
            SPUtils.putBoolean(Params.mIsOpenPrintSw, isChecked)
            DataApi.mIsOpenPrint = isChecked
        }

        mPrintTimesEt.addTextChangedListener(object : MyTextNumberWatcher() {
            override fun afterTextChanged(num: String) {
                if (num.isBlank()) {
                    SPUtils.putInt(Params.mPrintTimesEt, num.toInt())
                    DataApi.mPrintTimes = 0
                } else {
                    SPUtils.putInt(Params.mPrintTimesEt, num.toInt())
                    DataApi.mPrintTimes = num.toInt()
                }
            }
        })
    }

    override fun setOnLazyLoad(): Boolean = true
    override fun onLazyLoad() {
    }

}